package com.data.modules.online.auth.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.invoke.SerializedLambda;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.data.common.api.vo.Result;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.auth.entity.OnlAuthData;
import com.data.modules.online.auth.entity.OnlAuthPage;
import com.data.modules.online.auth.entity.OnlAuthRelation;
import com.data.modules.online.auth.service.IOnlAuthDataService;
import com.data.modules.online.auth.service.IOnlAuthPageService;
import com.data.modules.online.auth.service.IOnlAuthRelationService;
import com.data.modules.online.auth.vo.AuthColumnVO;
import com.data.modules.online.auth.vo.AuthPageVO;
import com.data.modules.online.cgform.entity.OnlCgformButton;
import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.entity.OnlCgformHead;
import com.data.modules.online.cgform.service.IOnlCgformButtonService;
import com.data.modules.online.cgform.service.IOnlCgformFieldService;
import com.data.modules.online.cgform.service.IOnlCgformHeadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "onlCgformAuthController")
@SuppressWarnings("ALL")
@RequestMapping("/online/cgform/api")
@RestController
public class OnlCgformAuthController {
    @Autowired
    private IOnlAuthDataService onlAuthDataService;
    @Autowired
    private IOnlAuthPageService onlAuthPageService;
    @Autowired
    private IOnlAuthRelationService onlAuthRelationService;
    @Autowired
    private IOnlCgformButtonService onlCgformButtonService;
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;

    private static /* synthetic */ Object m12a(SerializedLambda r4) {
        /*
        // Method dump skipped, instructions count: 2296
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.auth.p002a.OnlCgformAuthController.m12a(invoke.SerializedLambda):Object");
    }

    /** 表结构 数据权限
     * @param cgformId
     * @return
     */
    @ApiOperation("表结构 数据权限")
    @GetMapping({"/authData/{cgformId}"})
    public Result<List<OnlAuthData>> authDataList(@PathVariable("cgformId") String cgformId) {
        Result var2 = new Result();
        LambdaQueryWrapper<OnlAuthData> wrapper = new LambdaQueryWrapper<OnlAuthData>();
        wrapper.eq(OnlAuthData::getCgformId, cgformId);
        List var4 = this.onlAuthDataService.list(wrapper);
        var2.setResult(var4);
        var2.setSuccess(true);
        return var2;
    }

    @PostMapping({"/authData"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<OnlAuthData> authDataAdd(@RequestBody OnlAuthData onlAuthData) {
        Result<OnlAuthData> result = new Result<>();
        try {
            this.onlAuthDataService.save(onlAuthData);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    @PutMapping({"/authData"})
    public Result<OnlAuthData> authDataPut(@RequestBody OnlAuthData onlAuthData) {
        Result<OnlAuthData> result = new Result<>();
        this.onlAuthDataService.updateById(onlAuthData);
        result.success("编辑成功！");
        return result;
    }

    @DeleteMapping({"/authData/{id}"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<?> authDataDelete(@PathVariable("id") String str) {
        this.onlAuthDataService.deleteOne(str);
        return Result.ok("删除成功!");
    }

    /** 表结构 按钮权限
     * @param cgformId
     * @return
     */
    @ApiOperation("表结构 按钮权限")
    @GetMapping({"/authButton/{cgformId}"})
    public Result<Map<String, Object>> authButtonList(@PathVariable("cgformId") String cgformId) {
        Result result = new Result();
        LambdaQueryWrapper<OnlCgformButton> wrapper = new LambdaQueryWrapper<OnlCgformButton>();
        wrapper.eq(OnlCgformButton::getCgformHeadId, cgformId);
        wrapper.eq(OnlCgformButton::getButtonStatus, "1");
        wrapper.select(OnlCgformButton::getButtonCode, OnlCgformButton::getButtonName, OnlCgformButton::getButtonStyle);
        List var4 = this.onlCgformButtonService.list(wrapper);
        LambdaQueryWrapper<OnlAuthPage> var5 = new LambdaQueryWrapper<OnlAuthPage>();
        var5.eq(OnlAuthPage::getCgformId, cgformId);
        var5.eq(OnlAuthPage::getType, 2);
        List list = this.onlAuthPageService.list(var5);
        HashMap hashMap = new HashMap();
        hashMap.put("buttonList", var4);
        hashMap.put("authList", list);
        result.setResult(hashMap);
        result.setSuccess(true);
        return result;
    }

    @PostMapping({"/authButton"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<OnlAuthPage> mo107a(@RequestBody OnlAuthPage onlAuthPage) {
        boolean z;
        OnlAuthPage onlAuthPage2;
        Result<OnlAuthPage> result = new Result<>();
        try {
            String id = onlAuthPage.getId();
            if (!ConvertUtils.isNotEmpty(id) || (onlAuthPage2 = (OnlAuthPage) this.onlAuthPageService.getById(id)) == null) {
                z = false;
            } else {
                onlAuthPage2.setStatus(1);
                this.onlAuthPageService.updateById(onlAuthPage2);
                z = true;
            }
            if (!z) {
                onlAuthPage.setStatus(1);
                this.onlAuthPageService.save(onlAuthPage);
            }
            result.setResult(onlAuthPage);
            result.success("操作成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    @PutMapping({"/authButton/{id}"})
    public Result<?> mo115d(@PathVariable("id") String var1) {
        LambdaUpdateWrapper<OnlAuthPage> var2 = new UpdateWrapper<OnlAuthPage>().lambda();
        var2.eq(OnlAuthPage::getId, var1);
        var2.set(OnlAuthPage::getStatus, 0);
        this.onlAuthPageService.update(var2);
        return Result.ok("操作成功");
    }

    /** 表结构 字段权限
     * @param cgformId
     * @return
     */
    @ApiOperation("表结构 字段权限")
    @GetMapping({"/authColumn/{cgformId}"})
    public Result<List<AuthColumnVO>> authColumnList(@PathVariable("cgformId") String cgformId) {
        Result result = new Result();
        LambdaQueryWrapper<OnlCgformField> wrapper = new LambdaQueryWrapper<OnlCgformField>();
        wrapper.eq(OnlCgformField::getCgformHeadId, cgformId);
        wrapper.orderByAsc(OnlCgformField::getOrderNum);
        List list = this.onlCgformFieldService.list(wrapper);
        if (list == null || list.size() == 0) {
            Result.error("未找到对应字段信息!");
        }

        LambdaQueryWrapper<OnlAuthPage> var5 = new LambdaQueryWrapper<OnlAuthPage>();
        var5.eq(OnlAuthPage::getCgformId, cgformId);
        var5.eq(OnlAuthPage::getType, 1);
        List pageList = this.onlAuthPageService.list(var5);
        ArrayList arrayList = new ArrayList();
        Iterator iterator = list.iterator();

        while(iterator.hasNext()) {
            OnlCgformField var9 = (OnlCgformField)iterator.next();
            AuthColumnVO var10 = new AuthColumnVO(var9);
            Integer var11 = 0;
            boolean var12 = false;
            boolean var13 = false;
            boolean var14 = false;

            for(int var15 = 0; var15 < pageList.size(); ++var15) {
                OnlAuthPage var16 = (OnlAuthPage)pageList.get(var15);
                if (var9.getDbFieldName().equals(var16.getCode())) {
                    var11 = var16.getStatus();
                    if (var16.getPage() == 3 && var16.getControl() == 5) {
                        var12 = true;
                    }

                    if (var16.getPage() == 5) {
                        if (var16.getControl() == 5) {
                            var13 = true;
                        } else if (var16.getControl() == 3) {
                            var14 = true;
                        }
                    }
                }
            }

            var10.setStatus(var11);
            var10.setListShow(var12);
            var10.setFormShow(var13);
            var10.setFormEditable(var14);
            arrayList.add(var10);
        }

        result.setResult(arrayList);
        Result.ok("加载字段权限数据完成");
        return result;
    }

    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    @PutMapping({"/authColumn"})
    public Result<?> mo108a(@RequestBody AuthColumnVO authColumnVO) {
        Result<?> result = new Result<>();
        try {
            if (authColumnVO.getStatus().intValue() == 1) {
                this.onlAuthPageService.enableAuthColumn(authColumnVO);
            } else {
                this.onlAuthPageService.disableAuthColumn(authColumnVO);
            }
            result.success("操作成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PostMapping({"/authColumn"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<?> mo112b(@RequestBody AuthColumnVO authColumnVO) {
        Result<?> result = new Result<>();
        try {
            this.onlAuthPageService.switchAuthColumn(authColumnVO);
            result.success("操作成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /** 权限 分页列表
     * @param cgformId
     * @param num 1-字段 权限列表 其他是按钮 权限列表
     * @return
     */
    @ApiOperation("权限 分页列表")
    @GetMapping({"/authPage/{cgformId}/{type}"})
    public Result<List<AuthPageVO>> authPage(@PathVariable("cgformId") String cgformId, @PathVariable("type") Integer num) {
        Result<List<AuthPageVO>> result = new Result<>();
        result.setResult(this.onlAuthPageService.queryAuthByFormId(cgformId, num.intValue()));
        result.setSuccess(true);
        return result;
    }

    /** 查询 表单 数据权限列表
     * @param cgformId
     * @return
     */
    @ApiOperation("查询 表单 数据权限列表")
    @GetMapping({"/validAuthData/{cgformId}"})
    public Result<List<OnlAuthData>> validAuthData(@PathVariable("cgformId") String cgformId) {
        Result result = new Result();
        LambdaQueryWrapper<OnlAuthData> wrapper = new LambdaQueryWrapper<OnlAuthData>();
        wrapper.eq(OnlAuthData::getCgformId, cgformId);
        wrapper.eq(OnlAuthData::getStatus, 1);
        wrapper.select(OnlAuthData::getId, OnlAuthData::getRuleName);
        List list = this.onlAuthDataService.list(wrapper);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

@GetMapping({"/roleAuth"})
    public Result<List<OnlAuthRelation>> roleAuth(@RequestParam("roleId") String var1, @RequestParam("cgformId") String var2, @RequestParam("type") Integer var3, @RequestParam("authMode") String var4) {
        Result var5 = new Result();
        LambdaQueryWrapper<OnlAuthRelation> var6 = new LambdaQueryWrapper<OnlAuthRelation>();
        var6.eq(OnlAuthRelation::getRoleId, var1);
        var6.eq(OnlAuthRelation::getCgformId, var2);
        var6.eq(OnlAuthRelation::getType, var3);
        var6.eq(OnlAuthRelation::getAuthMode, var4);
        var6.select(OnlAuthRelation::getAuthId);
        List var7 = this.onlAuthRelationService.list(var6);
        var5.setResult(var7);
        var5.setSuccess(true);
        return var5;
    }

    @PostMapping({"/roleColumnAuth/{roleId}/{cgformId}"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<?> mo104a(@PathVariable("roleId") String str, @PathVariable("cgformId") String str2, @RequestBody JSONObject jSONObject) {
        Result<?> result = new Result<>();
        JSONArray jSONArray = jSONObject.getJSONArray("authId");
        this.onlAuthRelationService.saveRoleAuth(str, str2, 1, jSONObject.getString("authMode"), jSONArray.toJavaList(String.class));
        result.setSuccess(true);
        return result;
    }

    @PostMapping({"/roleButtonAuth/{roleId}/{cgformId}"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<?> mo110b(@PathVariable("roleId") String str, @PathVariable("cgformId") String str2, @RequestBody JSONObject jSONObject) {
        Result<?> result = new Result<>();
        JSONArray jSONArray = jSONObject.getJSONArray("authId");
        this.onlAuthRelationService.saveRoleAuth(str, str2, 2, jSONObject.getString("authMode"), jSONArray.toJavaList(String.class));
        result.setSuccess(true);
        return result;
    }

    @PostMapping({"/roleDataAuth/{roleId}/{cgformId}"})
    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:list", "sys:cache:online:form"})
    public Result<?> mo114c(@PathVariable("roleId") String str, @PathVariable("cgformId") String str2, @RequestBody JSONObject jSONObject) {
        Result<?> result = new Result<>();
        JSONArray jSONArray = jSONObject.getJSONArray("authId");
        this.onlAuthRelationService.saveRoleAuth(str, str2, 3, jSONObject.getString("authMode"), jSONArray.toJavaList(String.class));
        result.setSuccess(true);
        return result;
    }

    @GetMapping({"/getAuthColumn/{desformCode}"})
    public Result<List<AuthColumnVO>> mo118g(@PathVariable("desformCode") String var1) {
        OnlCgformHead var2 = (OnlCgformHead)this.onlCgformHeadService.getOne((Wrapper)(new LambdaQueryWrapper<OnlCgformHead>()).eq(OnlCgformHead::getTableName, var1));
        if (var2 == null) {
            Result.error("未找到对应字段信息!");
        }

        Result var3 = new Result();
        LambdaQueryWrapper<OnlCgformField> var4 = new LambdaQueryWrapper<OnlCgformField>();
        var4.eq(OnlCgformField::getCgformHeadId, var2.getId());
        var4.orderByAsc(OnlCgformField::getOrderNum);
        List var5 = this.onlCgformFieldService.list(var4);
        if (var5 == null || var5.size() == 0) {
            Result.error("未找到对应字段信息!");
        }

        ArrayList var6 = new ArrayList();
        Iterator var7 = var5.iterator();

        while(var7.hasNext()) {
            OnlCgformField var8 = (OnlCgformField)var7.next();
            AuthColumnVO var9 = new AuthColumnVO(var8);
            var9.setTableName(var2.getTableName());
            var9.setTableNameTxt(var2.getTableTxt());
            var6.add(var9);
        }

        if (ConvertUtils.isNotEmpty(var2.getSubTableStr())) {
            String[] var17 = var2.getSubTableStr().split(",");
            int var18 = var17.length;

            for(int var19 = 0; var19 < var18; ++var19) {
                String var10 = var17[var19];
                OnlCgformHead var11 = (OnlCgformHead)this.onlCgformHeadService.getOne((Wrapper)(new LambdaQueryWrapper<OnlCgformHead>()).eq(OnlCgformHead::getTableName, var10));
                if (var11 != null) {
                    LambdaQueryWrapper var12 = (LambdaQueryWrapper)(new LambdaQueryWrapper<OnlCgformField>()).eq(OnlCgformField::getCgformHeadId, var11.getId());
                    List var13 = this.onlCgformFieldService.list(var12);
                    if (var13 != null) {
                        Iterator var14 = var13.iterator();

                        while(var14.hasNext()) {
                            OnlCgformField var15 = (OnlCgformField)var14.next();
                            AuthColumnVO var16 = new AuthColumnVO(var15);
                            var16.setTableName(var11.getTableName());
                            var16.setTableNameTxt(var11.getTableTxt());
                            var6.add(var16);
                        }
                    }
                }
            }
        }

        var3.setResult(var6);
        Result.ok("加载字段权限数据完成");
        return var3;
    }
}